using System;
using System.Collections.Generic;
using System.Text;

namespace Multiplicity.Reflection {
    public class FieldInfo {

        string name;
        TypeDef declaringType;
        FieldSig signature;
        TypeInfo fieldType;

        internal FieldInfo(TypeDef type, uint index) {
            Module module = type.Module;
            MetadataTable<FieldRow> table = module.Tables.Fields;
            FieldRow row = table[index];

            this.name = module.Strings[row.NameIndex];
            this.declaringType = type;
            this.signature = new FieldSig(module.Blob[row.SignatureIndex]);
            
        }

        public string Name { get { return name; } }

        public TypeInfo FieldType {
            get {
                do {
                    TypeElement typeElement = signature.Type as TypeElement;
                    if (typeElement != null) {
                        fieldType = declaringType.Module.FindTypeInfo(typeElement.TypeToken);
                        break;
                    }
                } while (false);

                return fieldType;
            }
        }

        public override string ToString() {
            return string.Concat(signature.Type.ToString(), " ", name);
        }
    }
}
